package com.ctshk.rpc.goods.mapper.provider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.goods.dto.CommodityPageDTO;
import com.ctshk.rpc.order.goods.dto.resp.CommodityPageReq;
import org.apache.ibatis.jdbc.SQL;

/**
 * 商品分类-分页列表查询
 * @author 姜斌文
 * @since 2021-04-08
 */
public class CommodityProvider {

    /**
     * c端
     * @param page
     * @param pageReq
     * @return
     */
    public String findCommodityPageByConditionAndApp(IPage<CommodityPageDTO> page,CommodityPageReq pageReq){
        String sql = new SQL(){{
//            SELECT("vv.id,vv.commodity_classification_name,vv.commodity_classification_id, vv.commodity_name,vv.sale_status, vv.sort, vv.pack_status, vv.supplier_id,vv.supplier_name, vv.charge_person_id,vv.charge_person_name,vv.approval_status,vv.approval_id,vv.create_id,vv.gmt_create,\n" +
//                    "vv.file_url, vv.price, vv.stock, vv.label_type,vv.area_type");
//            FROM(
//                    SELECT("t.id,t.commodity_classification_name,t.commodity_classification_id, t.commodity_name,t.sale_status, t.sort, t.pack_status, t.supplier_id,t.supplier_name, t.charge_person_id,t.charge_person_name,t.approval_status,t.approval_id,t.create_id,t.gmt_create");
//                    SELECT("GROUP_CONCAT(file.file_url)");
//                    FROM("t_commodity_file file");
//                    WHERE("file.commodity_id = t.id and file.file_type = 2");
//                    )
////            FROM("(SELECT t.id,t.commodity_classification_name,t.commodity_classification_id, t.commodity_name,t.sale_status, t.sort, t.pack_status, t.supplier_id,t.supplier_name, t.charge_person_id,t.charge_person_name,t.approval_status,t.approval_id,t.create_id,t.gmt_create," +
//                    "(SELECT GROUP_CONCAT(file.file_url) FROM t_commodity_file file WHERE file.commodity_id = t.id and file.file_type = 2)  as  file_url," +
//                    "(SELECT MIN(cost.price) FROM t_commodity_cost cost WHERE cost.commodity_id = t.id and cost.commodity_attribute_id = cab.id)  as  price," +
//                    "(SELECT SUM(cost.stock) FROM t_commodity_cost cost WHERE cost.commodity_id = t.id and cost.commodity_attribute_id = cab.id)  as  stock," +
//                    "(SELECT GROUP_CONCAT( csa.area_type ) FROM t_commodity_sales_area csa WHERE csa.commodity_id = t.id AND csa.commodity_attribute_id = cab.id ) AS area_type," +
//                    "(SELECT GROUP_CONCAT(lat.label_type) FROM t_commodity_label lat WHERE lat.commodity_id = t.id)  as  label_type " +
//                    "FROM t_commodity t LEFT JOIN t_commodity_attribute cab ON ( cab.commodity_id = t.id ) WHERE  t.is_deleted = 0) vv");
//            WHERE("1 = 1");
//            if (null != pageReq.getCommodityName()) {
//                WHERE("(vv.commodity_name like concat('%',#{pageReq.commodityName},'%'))");
//            }
//            if (null != pageReq.getApprovalStatus()) {
//                WHERE("vv.approval_status = #{pageReq.approvalStatus} ");
//            }
//            if (null != pageReq.getApprovalId()) {
//                WHERE("vv.approval_id = #{pageReq.approvalId} ");
//            }
//            if (null != pageReq.getSaleStatus()) {
//                WHERE("vv.sale_status = #{pageReq.saleStatus} ");
//            }
//            if (null != pageReq.getCommodityClassificationId()) {
//                WHERE("vv.commodity_classification_id = #{pageReq.commodityClassificationId} ");
//            }
//            if (null != pageReq.getCommodityClassificationName()){
//                WHERE("vv.commodity_classification_name = #{pageReq.commodityClassificationName}");
//            }
//            if (null != pageReq.getLabelType() && pageReq.getLabelType().longValue() == 2){
//                WHERE("FIND_IN_SET(#{pageReq.labelType},vv.label_type) ");
//            }
//            if (null != pageReq.getSortType() && pageReq.getSortType().longValue() == 1) {
//                ORDER_BY("vv.sort ASC, vv.gmt_create desc");
//            } else if(null != pageReq.getSortType() && pageReq.getSortType().longValue() == 2){
//                ORDER_BY("vv.price ASC");
//            } else if(null != pageReq.getSortType() && pageReq.getSortType().longValue() == 3){
//                ORDER_BY("vv.gmt_create desc");
//            }
        }}.toString();
        return sql;
    }

    public String findCommodityPageByCondition(IPage<CommodityPageDTO> page, CommodityPageReq pageReq) {
    	String sql = new SQL() {{
            SELECT("vv.id, vv.commodity_name,vv.sale_status, vv.sort, vv.pack_status, vv.supplier_id,vv.supplier_name, vv.charge_person_id,vv.charge_person_name,vv.approval_status,vv.approval_id,vv.create_id,vv.gmt_create,\n" +
                    "vv.file_url, vv.price, vv.stock, vv.label_type,vv.area_type");
            FROM("(SELECT t.id, t.commodity_name,t.sale_status, t.sort, t.pack_status, t.supplier_id,t.supplier_name, t.charge_person_id,t.charge_person_name,t.approval_status,t.approval_id,t.create_id,t.gmt_create," +
                    "(SELECT GROUP_CONCAT(file.file_url) FROM t_commodity_file file WHERE file.commodity_id = t.id and file.file_type = 2)  as  file_url," +
                    "(SELECT MIN(cost.price) FROM t_commodity_cost cost WHERE cost.commodity_id = t.id and cost.commodity_attribute_id = cab.id)  as  price," +
                    "(SELECT SUM(cost.stock) FROM t_commodity_cost cost WHERE cost.commodity_id = t.id and cost.commodity_attribute_id = cab.id)  as  stock," +
                    "(SELECT GROUP_CONCAT( csa.area_type ) FROM t_commodity_sales_area csa WHERE csa.commodity_id = t.id AND csa.commodity_attribute_id = cab.id ) AS area_type," +
                    "(SELECT GROUP_CONCAT(lat.label_type) FROM t_commodity_label lat WHERE lat.commodity_id = t.id)  as  label_type " +
                    "FROM t_commodity t LEFT JOIN t_commodity_attribute cab ON ( cab.commodity_id = t.id ) WHERE  t.is_deleted = 0) vv");
            if (null != pageReq.getCommodityName()) {
                WHERE("(vv.commodity_name like concat('%',#{pageReq.commodityName},'%'))");
            }
            if (null != pageReq.getApprovalStatus()) {
                WHERE("vv.approval_status = #{pageReq.approvalStatus} ");
            }
            if (null != pageReq.getApprovalStatus()) {
                WHERE("vv.approval_status = #{pageReq.approvalStatus} ");
            }
            if (null != pageReq.getSaleStatus()) {
                WHERE("vv.sale_status = #{pageReq.saleStatus} ");
            }
            if (null != pageReq.getCommodityClassificationId()) {
                WHERE("vv.commodity_classification_id = #{pageReq.commodityClassificationId} ");
            }
            if (null != pageReq.getCommodityClassificationId()) {
                WHERE("vv.commodity_classification_id = #{pageReq.commodityClassificationId} ");
            }
            if (null != pageReq.getLabelType() && pageReq.getLabelType().longValue() == 2){
                WHERE("FIND_IN_SET(#{pageReq.labelType},vv.label_type) ");
            }
            if (null != pageReq.getSortType() && pageReq.getSortType().longValue() == 1) {
                ORDER_BY("vv.sort ASC, vv.gmt_create desc");
            } else if(null != pageReq.getSortType() && pageReq.getSortType().longValue() == 2){
                ORDER_BY("vv.price ASC");
            } else if(null != pageReq.getSortType() && pageReq.getSortType().longValue() == 3){
                ORDER_BY("vv.gmt_create desc");
            }
            }}.toString();
            return sql;
    }

    //========================== H5端 Start ==========================

    //========================== H5端 End ==========================
}
